import 'package:flutter/cupertino.dart';

class KeepAliveWrapperView extends StatefulWidget {
  final Widget child;
  final void Function()? callback;
  final bool keepAlive;
  const KeepAliveWrapperView(
      {super.key, required this.child, this.callback, this.keepAlive = true});

  @override
  State<KeepAliveWrapperView> createState() => _KeepAliveWrapperViewState();
}

class _KeepAliveWrapperViewState extends State<KeepAliveWrapperView>
    with AutomaticKeepAliveClientMixin {
  @override
  void initState() {
    widget.callback?.call();
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    super.build(context);
    return widget.child;
  }

  @override
  void didUpdateWidget(covariant KeepAliveWrapperView oldWidget) {
    if (oldWidget.keepAlive != widget.keepAlive) {
      // keepAlive 状态需要更新，实现在 AutomaticKeepAliveClientMixin 中
      updateKeepAlive();
    }
    super.didUpdateWidget(oldWidget);
  }

  @override
  bool get wantKeepAlive => widget.keepAlive;
}
